﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Manazra.Models.Repository;
using Manazra.Models;
using System.Web.Security;

namespace Manazra.Controllers.Filters
{
    public class AuthorizeFBOK : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //get the user authentication object in the view data
            UserAuth UserAuth = (UserAuth)filterContext.Controller.ViewData["UserAuth"];
            if (!UserAuth.Authenticated)
            {
                //get current url to redirect back
                string currentUrl = filterContext.HttpContext.Request.Url.AbsolutePath;

                //send user to login page
                string redirectUrl = String.Format("?ReturnUrl={0}", currentUrl);
                string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
